00001 // Emacs Mode Line: -*- Mode:c++;-*- 00002 // ------------------------------------------------------------- 00003 /* 00004 * Copyright (c) 2013 Battelle Memorial Institute 00005 * Licensed under modified BSD License. A copy of this license can be found 00006 * in the LICENSE file in the top level directory of this distribution. 00007 */ 00008 // ------------------------------------------------------------- 00009 /** 00010 * @file nonlinear_solver_functions.hpp 00011 * @author William A. Perkins 00012 * @date 2015-06-12 10:24:12 d3g096 00013 * 00014 * @brief Declaration of function (objects) used by nonlinear solvers 00015 * to build a Jacobian and RHS. 00016 * 00017 * 00018 */ 00019 #ifndef _nonlinear_solver_functions_hpp_ 00020 #define _nonlinear_solver_functions_hpp_ 00021 00022 #include <boost/function.hpp> 00023 #include <gridpack/math/matrix.hpp> 00024 00025 namespace gridpack { 00026 namespace math { 00027 00028 // ------------------------------------------------------------- 00029 // struct NLSBuilder 00030 // ------------------------------------------------------------- 00031 template <typename T, typename I = int> 00032 struct NLSBuilder { 00033 00034 /// A function object that builds the Jacobian for NonlinearSolver 00035 /** 00036 * This type is used to supply a way to build a Jacobian Matrix to 00037 * NonlinearSolver instances. The NonlinearSolver will use this each 00038 * iteration to construct the Matrix @c J from the current solution 00039 * estimate in @c x. A Jacobian builder may be a function or function 00040 * object. For example, this 00041 * 00042 @code{.cpp} 00043 void 00044 my_jacobian_builder(const Vector& x, Matrix& J) 00045 { 00046 // ... 00047 J.ready(); 00048 } 00049 JacobianBuilder j = my_jacobian_builder; 00050 @endcode 00051 * 00052 * and 00053 * 00054 \code{.cpp} 00055 struct my_jacobian_builder_type { 00056 void operator()(const Vector& x, Matrix& J) 00057 { 00058 // ... 00059 J.ready(); 00060 } 00061 } 00062 my_jacobian_builder_type my_jacobian_builder; 00063 JacobianBuilder j = my_jacobian_builder; 00064 \endcode 00065 * 00066 * are equivalent. The latter is usually more convenient because 00067 * necessary information can be included in the function object. 00068 */ 00069 typedef boost::function<void (const VectorT<T, I>& x, MatrixT<T, I>& J)> Jacobian; 00070 00071 /// A function object that builds the RHS for NonlinearSolver 00072 /** 00073 * This type is used to supply a way to build a right hand side Vector 00074 * to NonlinearSolver instances. The NonlinearSolver will use this 00075 * each iteration to construct the Vector @c F from the current 00076 * solution estimate in @c x. A FunctionBuilder may be a function or 00077 * function object. For example, this 00078 * 00079 @code{.cpp} 00080 void 00081 my_function_builder(const Vector& x, Vector& F) 00082 { 00083 // ... 00084 F.ready(); 00085 } 00086 JacobianBuilder j = my_function_builder; 00087 @endcode 00088 * 00089 * and 00090 * 00091 \code{.cpp} 00092 struct my_function_builder_type { 00093 void operator()(const Vector& x, Vector& F) 00094 { 00095 // ... 00096 F.ready(); 00097 } 00098 }; 00099 my_function_builder_type my_function_builder; 00100 JacobianBuilder j = boost::ref(my_function_builder); 00101 \endcode 00102 * 00103 * are equivalent. The latter is usually more convenient because 00104 * necessary information can be included in the function object. 00105 */ 00106 typedef boost::function<void (const VectorT<T, I>& x, VectorT<T, I>& F)> Function; 00107 00108 }; 00109 00110 typedef NLSBuilder<ComplexType>::Jacobian ComplexJacobianBuilder; 00111 typedef NLSBuilder<ComplexType>::Function ComplexFunctionBuilder; 00112 typedef NLSBuilder<RealType>::Jacobian RealJacobianBuilder; 00113 typedef NLSBuilder<RealType>::Function RealFunctionBuilder; 00114 00115 00116 } // namespace math 00117 } // namespace gridpack 00118 00119 #endif